<template>
  <div class="register">
    <el-form ref="registerForm" :model="registerForm" :rules="registerRules" class="register-form" label-position="top">
      <h3 class="title">用户注册</h3>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item prop="nickName"  label="真实姓名">
        <el-input v-model="registerForm.nickName" type="text" auto-complete="off" placeholder="请输入姓名">
        </el-input>
      </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="性别" prop="sex">
            <el-radio v-model="registerForm.sex" label="0">男</el-radio>
            <el-radio v-model="registerForm.sex" label="1">女</el-radio>
          </el-form-item>
        
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item prop="phonenumber"  label="手机号">
   <div style="display: flex;">

    <div>
      <el-input v-model="registerForm.phonenumber" type="text" auto-complete="off" style="width: 265px;" placeholder="请输入手机号">
        </el-input>
     </div>
           <div class="login-code">
          <el-button round @click.native.prevent="getSmsCode">{{computeTime>0 ? `(${computeTime}s)已发送` : '获取验证码'}}</el-button>
        </div>
   </div>
      </el-form-item>
        </el-col>
        <!-- <el-col :span="12">
          <el-form-item prop="code" label="验证码">
        <el-input
          v-model="registerForm.code"
          auto-complete="off"
          placeholder="验证码"
          style="width: 43%"
          
        >
          <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
        </el-input>
     
      </el-form-item>
        </el-col> -->
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12" style="display: flex;">
          <span style="margin-top: 12px;margin-right: 5px;color: red;">*</span>
         <el-form-item prop="password"  label="验证码">
        <el-input
          v-model="registerForm.password"
          type="text" 
          auto-complete="off"
          placeholder="请输入验证码"
          style="width: 260px;"
        
        >
          <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
        </el-col>
      </el-row>
     
      
      <!-- <el-form-item prop="code" v-if="captchaEnabled">
        <el-input  
          v-model="registerForm.code"
          auto-complete="off"
          placeholder="验证码"
          style="width: 63%"
          @keyup.enter.native="handleRegister"
        >
          <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
        </el-input>
        <div class="register-code">
          <img :src="codeUrl" @click="getCode" class="register-code-img"/>
        </div>
      </el-form-item> -->
      <el-form-item style="width:100%;">
        <el-button
          :loading="loading"
          size="medium"
          type="primary"
          style="width:100%;"
          @click.native.prevent="handleRegister"
        >
          <span v-if="!loading">注 册</span>
          <span v-else>注 册 中...</span>
        </el-button>
        <div style="float: right;">
          <router-link class="link-type" :to="'/login'">使用已有账户登录</router-link>
        </div>
      </el-form-item>
    </el-form>
    <!--  底部  -->


    <button @click="goshouquan()">微信授权</button>
    <div class="el-register-footer">
      <span></span>
    </div>
  </div>
</template>

<script>
// import { getCodeImg, register } from "@/api/login";
import {zhuce,getzhucema,shouquan} from "@/api/system/zhuce";

export default {
  name: "Register",
  data() {
    // const equalToPassword = (rule, value, callback) => {
    //   if (this.registerForm.password !== value) {
    //     callback(new Error("两次输入的密码不一致"));
    //   } else {
    //     callback();
    //   }
    // };
    return {
      computeTime: 0,
      timer:null,

      codeUrl: "",
      registerForm: {
        nickName: "",
        password: "",
        confirmPassword: "",
        phonenumber:"",
        sex:"",
        email:"",
        code:""
      },
      registerRules: {
        phonenumber: [
          {
            required: true, message: "手机号不能为空", trigger: "blur",
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur"
          }
        ],
        sex: [
          { required: true, message: "性别不能为空", trigger: "change" }
        ],
        nickName: [
        { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
        // code:[
        // { required: true, message: "验证码不能为空", trigger: "blur" }
        // ]
        // password: [
        //   { required: true, trigger: "blur", message: "请输入您的密码" },
        //   { min: 5, max: 20, message: '用户密码长度必须介于 5 和 20 之间', trigger: 'blur' }
        // ],
        // confirmPassword: [
        //   { required: true, trigger: "blur", message: "请再次输入您的密码" },
        //   { required: true, validator: equalToPassword, trigger: "blur" }
        // ],
        // code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      captchaEnabled: true,
      url:""
    };
  },
  created() {
    // this.getCode();
    // this.getshouquan()
  },
  methods: {
    getshouquan(){
      shouquan({}).then(res=>{
        console.log(res)
        this.url=res.data
      })
    },
    goshouquan(){
      // window.location.href = this.url
      window.location.href= "https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid=wx87e049c842083caa&pre_auth_code=preauthcode@@@SMkhlNzXWrkLQDiLULI8sMaPyQSAlWlacsX93cctecYSFf0c69y9DA_599dpTQWMtVHvG6gjfXQkdVCB0hS7cA&redirect_uri=http://ftsk.natapp1.cc/openplatform/callBack"
    },
    getSmsCode(){
      if (!this.computeTime) {
       this.$refs.registerForm.validate(valid => {
         if (valid) {
          getzhucema({
            username:this.registerForm.phonenumber
          }).then(res =>{
              if(res.code === 200){
                this.$message({
                        message: '验证码已发送',
                        type: 'success'
                      });
              // this.loginForm.uuid = res.uuid;
              this.computeTime = 60;
              this.timer = setInterval(() => {
              	this.computeTime--;
              	if (this.computeTime <= 0) {
              		clearInterval(this.timer)
              	}
              }, 1000);
              }
            })
         }
      })
     }
    },
    handleRegister() {
      this.$refs.registerForm.validate(valid => {
        if (valid) {
       if(this.registerForm.password){
        this.loading = true;
        console.log(this.registerForm);
        zhuce(this.registerForm).then(res=>{
             const phonenumber = this.registerForm.phonenumber;
            this.$alert("<font color='red'>恭喜你，您的手机号" + phonenumber + " 注册成功！</font>", '系统提示', {
              dangerouslyUseHTMLString: true,
              type: 'success',
              confirmButtonText:"去登录"
            }).then(() => {
              this.$router.push("/login");
            }).catch(() => {});
          }).catch(() => {
            this.loading = false;
           
        })
       }else{
        this.$message({
                        message: '请输入验证码',
                        type: 'error'
                      });
       }
        }
      });
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
.register {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url("../assets/images/login-background.jpg");
  background-size: cover;
}
.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}

.register-form {
  border-radius: 6px;
  background: #ffffff;
  width: 600px;
  padding: 25px 25px 5px 25px;
  .el-input {
    height: 38px;
    input {
      height: 38px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}
.login-code {
  width: 33%;
  height: 38px;
  float: right;
  margin-left: 20px;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.register-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.register-code {
  width: 33%;
  height: 38px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-register-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.register-code-img {
  height: 38px;
}
</style>
